package com.eryos.android.cigarettecounter.database;

import java.util.ArrayList;
import java.util.List;

/**
 * Project : CigaretteCounter Author : Olivier Comb e URL : http://eryos.fr/CigaretteCounter/
 * 
 * Copyright(C)2011 - Olivier Combe
 * 
 * Licensed under the Apache License, Version 2.0(the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 * 
 * @version \$Revision.*\$
 * @author Olivier Combe
 */
public class SQLQuery {

    private final List<String> selectClause;
    private final List<String> whereClause;
    private final List<String> groupClause;
    private final List<String> orderClause;
    private final String fromClause;

    /**
     * 
     */
    public SQLQuery() {
        selectClause = new ArrayList<String>();
        whereClause = new ArrayList<String>();
        groupClause = new ArrayList<String>();
        orderClause = new ArrayList<String>();
        fromClause = "FROM " + DBConnector.DATABASE_NAME + " ";
    }

    /**
     * Set the SELECT clause of the SQL query.
     * 
     * @param selectList
     *            list of items
     */
    public void setSelect(final String... selectList) {
        for (String select : selectList) {
            selectClause.add(select);
        }
    }

    /**
     * Set the WHERE clause of the SQL query.
     * 
     * @param selectList
     *            list of items
     */
    public void setWhere(final String... selectList) {
        for (String select : selectList) {
            whereClause.add(select);
        }
    }

    /**
     * Set the ORDER part of the SQL query.
     * 
     * @param selectList
     *            list of items
     */
    public void setOrder(final String... selectList) {
        for (String select : selectList) {
            orderClause.add(select);
        }
    }

    /**
     * Set the GROUPING part of the SQL query.
     * 
     * @param selectList
     *            list of items
     */
    public void setGroup(final String... selectList) {
        for (String select : selectList) {
            groupClause.add(select);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        String select = "SELECT ";
        for (String att : selectClause) {
            select += att + ", ";
        }
        select = select.substring(0, select.length() - 2);

        String where = "WHERE ";
        for (String att : whereClause) {
            where += att + " ";
        }

        String order = "";
        if (orderClause.size() > 0) {
            order = "ORDER BY ";
            for (String att : orderClause) {
                order += att + ", ";
            }
            order = order.substring(0, select.length() - 2);
        }

        String group = "";
        if (groupClause.size() > 0) {
            group = "GROUP BY ";
            for (String att : groupClause) {
                group += att + ", ";
            }
            group = group.substring(0, select.length() - 2);
        }

        return select + " " + fromClause + " " + where + " " + group + " " + order + ";";
    }
}
